//
//  HWIntegralMallViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/9.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWIntegralMallViewController.h"
#import "HWXuNiShopViewController.h"  // 虚拟商品
#import "HWShiWuShangPingViewController.h" //  实物商品
#import "HWIntegralSubsidiaryViewController.h" // 积分明细
#import "HWLoginController.h" // 登录界面

@interface HWIntegralMallViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UILabel *jifenLabel; //积分Label
@property(strong,nonatomic) UIButton *shuxing111But; // 刷新

//滚动试图
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (strong,nonatomic) UIImageView  *xiantiaoimageview;
@property (nonatomic,strong) UIButton *selectButton;
@property (nonatomic, strong) NSMutableArray *titleButtons;
@property (nonatomic,strong)  NSMutableArray *biaotiArray;

@end

@implementation HWIntegralMallViewController

- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
   // [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self.navigationController setNavigationBarHidden:YES];
   
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
    
    
}


- (void)shuxingunderRefresh {
    
   
    
  //  self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

 //   self.dataMuArr = [[NSMutableArray alloc]init];
     NSDictionary *parametes = @{
                                       @"p" : @"1",
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"mall",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"type" :typeStr,
                                       @"sign" : singMD5
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
        
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
            
                 NSString *miaoShu = [NSString stringWithFormat:@"我的积分：%@",response[@"message"]];
               
                NSString *jifen = [NSString stringWithFormat:@"%@",response[@"message"]];
               
                  CGSize size =  [WMUtil strName:miaoShu fontSize:[UIFont systemFontOfSize:14] boundingRectWithSize:CGSizeMake(1000, 200)];
               // self.jifenLabel.backgroundColor = [UIColor redColor];
                 self.jifenLabel.attributedText = [WMUtil  attributedStringWithText:miaoShu withRangText:jifen withColor:RGB(253, 178, 50) withFont:[UIFont systemFontOfSize:14]];
                self.jifenLabel.frame =  CGRectMake(12, 0, size.width+5, 40);
               //   CGRectMake(12, 0, size.width, 40)
               
               self.shuxing111But.frame = CGRectMake(self.jifenLabel.right, 0, 30, 40);
               
              // self.shuxingBut.frame = CGRectMake(self.jifenLabel.right, 0, 30, 40);
               
    
           } else  {
               
               
               
           }
        
           
           
       } failure:^(NSString *error) {
          
           
           
       }];
    
    
}








-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {


        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(15, 165+CF_StatusBarHeight+20+8, kScreenW-30,58*5) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        

    }
    return   _mainaTableVie ;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
}

- (void)setupUI {
   
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = RGB(242, 242, 242);
    [self setUPbeijingView];
    
    [self navigaView];
//
//    self.mainaTableVie.layer.cornerRadius = 3;
//    self.mainaTableVie.layer.masksToBounds = YES;
//    self.mainaTableVie.scrollEnabled = NO;
//    [self.view addSubview:self.mainaTableVie];
    
    // 1.添加标题滚动视图
       [self setupTitleScrollView];
    // 2.添加内容滚动视图
    [self setupContentScrollView];
       
    [self  setupAllChildViewController];
    [self setupAllTitle];
    
    
    //查询积分
   [self shuxingunderRefresh];
    
    
    
}





- (void)setUPbeijingView {
    
    UIView *beijingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 165+CF_StatusBarHeight+20)];
    beijingView.backgroundColor = RGB(242, 242, 242);
     
    [self.view addSubview:beijingView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 165+CF_StatusBarHeight)];
    imageView.image = [UIImage imageNamed:@"welfare_bg"];
    [beijingView addSubview:imageView];
    
    
    UIImageView *jingxiang = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-149-10, 165+CF_StatusBarHeight-124-5, 149, 124)];
    jingxiang.image = [UIImage imageNamed:@"welfare_con"];
   // jingxiang.backgroundColor = [UIColor yellowColor];
    
    [imageView addSubview:jingxiang];
    
    
    UIImageView *renwujifenwenziimageview = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-147*CFLScaleValue-149-10)/2, 165+CF_StatusBarHeight-50*CFLScaleValue-40, 147*CFLScaleValue, 50*CFLScaleValue)];
    renwujifenwenziimageview.image = [UIImage imageNamed:@"welfare_message_tit"];
   // renwujifenwenziimageview.backgroundColor = [UIColor redColor];
    [imageView addSubview:renwujifenwenziimageview];
       
    
    
    UIView *jifenView = [[UIView alloc]initWithFrame:CGRectMake(15, 165+CF_StatusBarHeight+20-40, kScreenW-15*2, 40)];
    jifenView.backgroundColor = [UIColor whiteColor];
    jifenView.layer.cornerRadius = 6;
    jifenView.layer.masksToBounds = YES;
    [beijingView addSubview:jifenView];
    
//    UIButton *jifengbuton =  [UIButton buttonWithType:UIButtonTypeCustom];
//    [jifengbuton setTitle:@"积分商城" forState:UIControlStateNormal];
//    jifengbuton.frame = CGRectMake( kScreenW-15*2-73-15, 8, 73, 25);
//    jifengbuton.layer.cornerRadius = 12;
//    jifengbuton.layer.masksToBounds = YES;
//    jifengbuton.backgroundColor = RGB(233, 241, 255);
//    jifengbuton.titleLabel.font = [UIFont systemFontOfSize:12];
//    [jifengbuton setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
//    [jifengbuton addTarget:self action:@selector(jifengbutonTap) forControlEvents:UIControlEventTouchUpInside];
//
//    [jifenView addSubview:jifengbuton];
    
    
        UIButton *huqujifenmiashu =  [UIButton buttonWithType:UIButtonTypeCustom];
        [huqujifenmiashu setTitle:@"如何获取积分" forState:UIControlStateNormal];
        huqujifenmiashu.frame = CGRectMake( kScreenW-15*2-80-13, 5, 80, 30);
        huqujifenmiashu.layer.cornerRadius = 12;
        huqujifenmiashu.layer.masksToBounds = YES;
       // huqujifenmiashu.backgroundColor = [UIColor redColor];
      //  huqujifenmiashu.backgroundColor = RGB(233, 241, 255);
        huqujifenmiashu.titleLabel.font = [UIFont systemFontOfSize:12];
        [huqujifenmiashu setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
        [huqujifenmiashu addTarget:self action:@selector(huiquTap) forControlEvents:UIControlEventTouchUpInside];
    
        [jifenView addSubview:huqujifenmiashu];
    
    
      UIButton *mingXiBut =  [UIButton buttonWithType:UIButtonTypeCustom];
      [mingXiBut setTitle:@"积分明细" forState:UIControlStateNormal];
      mingXiBut.frame = CGRectMake( kScreenW-15*2-80-60-10-13, 5, 60, 30);
      mingXiBut.layer.cornerRadius = 12;
      mingXiBut.layer.masksToBounds = YES;
    // mingXiBut.backgroundColor = [UIColor redColor];
         //  huqujifenmiashu.backgroundColor = RGB(233, 241, 255);
     mingXiBut.titleLabel.font = [UIFont systemFontOfSize:12];
      [mingXiBut setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
     [mingXiBut addTarget:self action:@selector(mingXiButTap) forControlEvents:UIControlEventTouchUpInside];
     [jifenView addSubview:mingXiBut];
        
     
    
    
    
    CGSize size =  [WMUtil strName:@"我的积分：500" fontSize:[UIFont systemFontOfSize:14] boundingRectWithSize:CGSizeMake(1000, 200)];
    self.jifenLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, size.width, 40)];
    self.jifenLabel.text = @"我的积分：500";
    self.jifenLabel.font = [UIFont systemFontOfSize:14];
    self.jifenLabel.textColor = RGB(51, 51, 51);
    [jifenView addSubview:   self.jifenLabel];

     self.jifenLabel.attributedText = [WMUtil  attributedStringWithText:@"我的积分：0" withRangText:@"0" withColor:RGB(253, 178, 50) withFont:[UIFont systemFontOfSize:14]];
   
    
    UIButton *shulianbuton =  [UIButton buttonWithType:UIButtonTypeCustom];
    shulianbuton.frame = CGRectMake(self.jifenLabel.right+5, 0, 30, 40);
    [shulianbuton setImage:[UIImage imageNamed:@"welfare_refresh"] forState:UIControlStateNormal];
//    self.shuxingBut = shulianbuton;
    self.shuxing111But = shulianbuton;
    
    [shulianbuton addTarget:self action:@selector(shuxingBut) forControlEvents:UIControlEventTouchUpInside];
    
    [jifenView addSubview:shulianbuton];
    
}

#pragma mark -------  刷新

- (void)shuxingBut {
    
    
    DLog(@" shuxingBut");
}

- (void)huiquTap {
    
    
    DLog(@"如何获取积分");
    
    
}

- (void)mingXiButTap  {
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
          HWLoginController  *login = [[HWLoginController alloc]init];
            [self.navigationController pushViewController:login animated:YES];
           return;
    
    }
    
    HWIntegralSubsidiaryViewController *IntegralSubsidiary = [[HWIntegralSubsidiaryViewController alloc]init];
    [self.navigationController pushViewController:IntegralSubsidiary animated:YES];
    
    
}





- (void)navigaView  {
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+17, 40, 17);
   // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+17, 70, 17)];
    textlabel.text = @"积分商城";
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:textlabel];
    
    
    
    
    
  //  jiludushao
    
//       UIButton *lishijilubutton = [UIButton buttonWithType:UIButtonTypeCustom];
//       lishijilubutton.frame = CGRectMake(kScreenW-35,StatusBarHeight+17, 17, 17);
//      // button.backgroundColor = [UIColor redColor];
//       [lishijilubutton setImage:[UIImage imageNamed:@"jiludushao"] forState:UIControlStateNormal];
//       [lishijilubutton addTarget:self action:@selector(jiludushaoTap) forControlEvents:UIControlEventTouchUpInside];
//       [self.view addSubview:lishijilubutton];
    
    
    
    
    
    
    
    
}




- (void)buttonTap {
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark ------------  添加的滚动Uiview




- (void)setupTitleScrollView
{
    // 创建titleScrollView
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = 165+CF_StatusBarHeight+20+8;
   // titleScrollView.backgroundColor = [UIColor redColor];
    
    titleScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, 40);
    titleScrollView.backgroundColor = [UIColor whiteColor];
   // titleScrollView.backgroundColor = RGB(217, 247, 250);
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
    
   
    
    
    
}

#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 创建contentScrollView
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
   // contentScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height - y);
    contentScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height-y);
    
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
    
    // 设置contentScrollView的属性
    // 分页
    self.contentScrollView.pagingEnabled = YES;
    // 弹簧
    self.contentScrollView.bounces = NO;
    // 指示器
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    contentScrollView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理.目的:监听内容滚动视图 什么时候滚动完成
    self.contentScrollView.delegate = self;
    
    
    
    
    
}






#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    self.biaotiArray = [[NSMutableArray alloc]init];
    [self.biaotiArray addObject:@"虚拟商品"];
    [self.biaotiArray addObject:@"实物商品"];
    
    for (int i = 0; i < self.biaotiArray.count; i++) {
        if (i ==  0) {
            HWXuNiShopViewController * jingrikaifu = [[HWXuNiShopViewController alloc]init];
            [self addChildViewController:jingrikaifu];
            
            
            
        } else {
            

            HWShiWuShangPingViewController *mingrikaifu = [[HWShiWuShangPingViewController alloc]init];
            [self addChildViewController:mingrikaifu];
            
    
            
        }
    }
    
    
}


#pragma mark - 设置所有标题
- (void)setupAllTitle
{
    
    
    UIView *fegexiaView = [[UIView alloc]initWithFrame:CGRectMake(0, 39,kScreenW , 1)];
    fegexiaView.backgroundColor = RGB(242, 242, 242);
    [self.titleScrollView addSubview:fegexiaView];
    
    // 已经把内容展示上去 -> 展示的效果是否是我们想要的(调整细节)
    // 1.标题颜色 为黑色
    // 2.需要让titleScrollView可以滚动
    
    // 添加所有标题按钮
    NSInteger count = self.biaotiArray.count;
    CGFloat btnW = 100;
    CGFloat btnH = self.titleScrollView.bounds.size.height;
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        //  UIViewController *vc = self.childViewControllers[i];
        [titleButton setTitle:self.biaotiArray[i] forState:UIControlStateNormal];
        CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
        
        btnX = i * btnW + jiaju*(i+1);
        
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //    titleButton.backgroundColor = [UIColor redColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        // 把标题按钮保存到对应的数组
        [self.titleButtons addObject:titleButton];
        
    
        
        if (i == 0) {
            [self titleClick:titleButton];
            
            UIImageView *xiantiaoimageview = [[UIImageView alloc]initWithFrame:CGRectMake(btnX+20, btnH-2, 60, 2)];
            
            xiantiaoimageview.image = [UIImage imageNamed:@""];
            xiantiaoimageview.backgroundColor = RGB(26, 113, 255);
            xiantiaoimageview.layer.cornerRadius = 1;
            xiantiaoimageview.layer.masksToBounds = YES;
            self.xiantiaoimageview = xiantiaoimageview;
            [self.titleScrollView addSubview:xiantiaoimageview];
            
        }
        
        [self.titleScrollView addSubview:titleButton];
    }
    
    

    
    
    
    
    
    
    // 设置标题的滚动范围
    self.titleScrollView.contentSize = CGSizeMake(kScreenW, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(count * kScreenW, 0);
    
    // bug:代码跟我的一模一样,但是标题就是显示不出来
    // 内容往下移动,莫名其妙
}


#pragma mark - 处理标题点击
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.标题颜色 变成 红色
    [self selButton:button];
    
 //    CGRectMake(btnX+25, btnH-2-6, 50, 2)
    
   
    // 2.把对应子控制器的view添加上去
    [self setupOneViewController:i];
    
    // 3.内容滚动视图滚动到对应的位置
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

#pragma mark - 选中标题
- (void)selButton:(UIButton *)button
{
    [self.selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
    self.selectButton = button;
    
    NSInteger i = button.tag;
    NSInteger count = self.biaotiArray.count;
    CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
    CGFloat btnW = 100;
    CGFloat btnX = i * btnW + jiaju*(i+1);
    self.xiantiaoimageview.frame = CGRectMake(btnX+20, 40-2, 60, 2);
    
    
    
    
    
    
}

#pragma mark - 添加一个子控制器的View
- (void)setupOneViewController:(NSInteger)i
{
    
    UIViewController *vc = self.childViewControllers[i];
    if (vc.view.superview) {
        return;
    }
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    vc.view.frame = CGRectMake(x, 0,   kScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
}





// 只要一滚动就需要字体渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 字体缩放 1.缩放比例 2.缩放哪两个按钮
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;

    UIButton *leftBtn = self.titleButtons[leftI];

    // 1.标题颜色 变成 红色
    [self selButton:leftBtn];

    // 添加控制器
    [self setupOneViewController:leftI];


}





@end
